import { numReg } from '@/utils/regexp';
const arab_han = '零一二三四五六七八九'
const arab_han_tel = '零幺二三四五六七八九'

/**
 * 阿拉伯数字转换为简写汉字
 * @param {number | string} num
 * @returns {string}
 */
const arab2han = (_num: number | string, options?: { tel: boolean }) => {
  const numstr = String(_num)
  let hanstr = ''
  if (!numReg.test(numstr)) {
    throw new Error('param not number string')
  }
  for (let i = 0; i < numstr.length; i++) {
    const char = numstr[i]
    if (char === '.') {
      hanstr += '点'
    } else if (char === '-') {
      hanstr += options?.tel ? '' : '负'
    } else {
      hanstr += options?.tel ? arab_han_tel[Number(char)] : arab_han[Number(char)]
    }
  }
  return hanstr
};

export default arab2han
